package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessEvaluate;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.TrainEvaluateNewVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.xml.CommonXML;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/5/8 14:15
 * 修改备注：
 * ================================================
 */
public class TrainEvaluateNewActivity extends BaseActivity {


    @Bind(R.id.train_evaluate_layout)
    LinearLayout trainEvaluateLayout;
    @Bind(R.id.train_evaluate_sv)
    ScrollView trainEvaluateSv;
    @Bind(R.id.view_train_evaluate_edt)
    EditText viewTrainEvaluateEdt;
    @Bind(R.id.view_train_evaluate_btn_submit)
    Button viewTrainEvaluateBtnSubmit;
    @Bind(R.id.train_evaluate_layout_parent)
    LinearLayout trainEvaluateLayoutParent;

    public static final int NUM = 6;

    private String arrayTitle[] = new String[NUM];
    private String code[] = {"", "", "", "", "", ""};
    private List<List<TrainEvaluateNewVO>> list = new ArrayList<>();
    private String trainingId; // 培训ID

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_train_evaluate_new);
        setTitle("培训评价");
    }

    @Override
    public void initMember() {
        if (StringUtil.isStrEmpty(CommonXML.getToEvaluate(this)))
            // 调用评价选项接口
            BusinessEvaluate.toEvaluate(this, mHandler);
        else
            setView(CommonXML.getToEvaluate(this));


        if (getIntent().hasExtra("JPush_evaluate")) {
            trainingId = (String) getIntent().getSerializableExtra("JPush_evaluate");
        } else {
            trainingId = (String) getIntent().getSerializableExtra("evaluateDetail");
            // 展示已经评价的内容
        }
    }

    @Override
    public void addListener() {
        viewTrainEvaluateBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < NUM; i++) {
                    if (StringUtil.isStrEmpty(code[i])) {
                        showToast("请完善评价-" + arrayTitle[i]);
                        return;
                    }
                }
//                BusinessEvaluate.evaluateForTrainNew(TrainEvaluateNewActivity.this,);
                showToast("提交接口");
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_TO_EVALUATE_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_TO_EVALUATE_FIELD:

                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        if (object == null)
            return;
        CommonXML.setToEvaluate(this, object.toString());
        setView(object.toString());

    }

    /**
     * 设置视图
     *
     * @param json
     */
    private void setView(String json) {

        // 课程安排
        TrainEvaluateNewVO courseArrange = FastJsonUtil.getBean(json, "courseArrange", TrainEvaluateNewVO.class);
        List<TrainEvaluateNewVO> courseArrangeList = FastJsonUtil.getListBean(json, "courseArrangeList", TrainEvaluateNewVO.class);
        // 教学方法
        TrainEvaluateNewVO teachMethod = FastJsonUtil.getBean(json, "teachMethod", TrainEvaluateNewVO.class);
        List<TrainEvaluateNewVO> teachMethodList = FastJsonUtil.getListBean(json, "teachMethodList", TrainEvaluateNewVO.class);
        // 教学手段
        TrainEvaluateNewVO teachMeans = FastJsonUtil.getBean(json, "teachMeans", TrainEvaluateNewVO.class);
        List<TrainEvaluateNewVO> teachMeansList = FastJsonUtil.getListBean(json, "teachMeansList", TrainEvaluateNewVO.class);
        // 表达技巧
        TrainEvaluateNewVO expressionSkill = FastJsonUtil.getBean(json, "expressionSkill", TrainEvaluateNewVO.class);
        List<TrainEvaluateNewVO> expressionSkillList = FastJsonUtil.getListBean(json, "expressionSkillList", TrainEvaluateNewVO.class);
        // 教学互动
        TrainEvaluateNewVO teachInteraction = FastJsonUtil.getBean(json, "teachInteraction", TrainEvaluateNewVO.class);
        List<TrainEvaluateNewVO> teachInteractionList = FastJsonUtil.getListBean(json, "teachInteractionList", TrainEvaluateNewVO.class);
        // 组织工作
        TrainEvaluateNewVO organizeWork = FastJsonUtil.getBean(json, "organizeWork", TrainEvaluateNewVO.class);
        List<TrainEvaluateNewVO> organizeWorkList = FastJsonUtil.getListBean(json, "organizeWorkList", TrainEvaluateNewVO.class);

        if (courseArrangeList == null || courseArrangeList.size() == 0 || teachMethodList == null || teachMethodList.size() == 0 || teachMeansList == null || teachMeansList.size() == 0 || expressionSkillList == null ||
                expressionSkillList.size() == 0 || teachInteractionList == null || teachInteractionList.size() == 0 || organizeWorkList == null || organizeWorkList.size() == 0) {
            showToast("数据获取失败");
            CommonXML.setToEvaluate(this, "");
            return;
        }


        arrayTitle[0] = courseArrange != null && courseArrange.getName() != null ? courseArrange.getName() : "";
        arrayTitle[1] = teachMethod != null && teachMethod.getName() != null ? teachMethod.getName() : "";
        arrayTitle[2] = teachMeans != null && teachMeans.getName() != null ? teachMeans.getName() : "";
        arrayTitle[3] = expressionSkill != null && expressionSkill.getName() != null ? expressionSkill.getName() : "";
        arrayTitle[4] = teachInteraction != null && teachInteraction.getName() != null ? teachInteraction.getName() : "";
        arrayTitle[5] = organizeWork != null && organizeWork.getName() != null ? organizeWork.getName() : "";

        list.add(courseArrangeList);
        list.add(teachMethodList);
        list.add(teachMeansList);
        list.add(expressionSkillList);
        list.add(teachInteractionList);
        list.add(organizeWorkList);

        for (int i = 0; i < NUM; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.view_train_evaluate_new, null);
            TextView tvTitle = (TextView) view.findViewById(R.id.view_train_evaluate_tv_title);
            TextView tvType1 = (TextView) view.findViewById(R.id.view_train_evaluate_tv_type1);
            TextView tvType2 = (TextView) view.findViewById(R.id.view_train_evaluate_tv_type2);
            TextView tvType3 = (TextView) view.findViewById(R.id.view_train_evaluate_tv_type3);
            TextView tvType4 = (TextView) view.findViewById(R.id.view_train_evaluate_tv_type4);

            tvTitle.setText(arrayTitle[i]);
            tvType1.setText(list.get(i).get(0).getName());
            tvType2.setText(list.get(i).get(1).getName());
            tvType3.setText(list.get(i).get(2).getName());
            tvType4.setText(list.get(i).get(3).getName());

            final TextView arrayTextView[] = new TextView[4];
            arrayTextView[0] = tvType1;
            arrayTextView[1] = tvType2;
            arrayTextView[2] = tvType3;
            arrayTextView[3] = tvType4;

            final int finalI = i;
            tvType1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setTextColor(arrayTextView, 0);
                    code[finalI] = list.get(finalI).get(0).getCode();
                }
            });
            tvType2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setTextColor(arrayTextView, 1);
                    code[finalI] = list.get(finalI).get(1).getCode();
                }
            });
            tvType3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setTextColor(arrayTextView, 2);
                    code[finalI] = list.get(finalI).get(2).getCode();
                }
            });
            tvType4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setTextColor(arrayTextView, 3);
                    code[finalI] = list.get(finalI).get(3).getCode();
                }
            });

            trainEvaluateLayout.addView(view);
        }
        trainEvaluateLayoutParent.setVisibility(View.VISIBLE);
    }

    /**
     * 设置选中后的颜色
     *
     * @param index
     */
    private void setTextColor(TextView textView[], int index) {
        for (int i = 0; i < 4; i++) {
            textView[i].setTextColor(getResources().getColor(R.color.colorGray));
            textView[i].setBackgroundResource(R.drawable.shape_gray_radius_2dp);
        }

        textView[index].setTextColor(getResources().getColor(R.color.app_color));
        textView[index].setBackgroundResource(R.drawable.shape_app_color_width_2dp_radius_2dp);

    }


    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
